
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File  :messages_push.py
# Time  :2024/11/26 16:28
# Email :fortunatedong@163.com
# Author:by Adonli
from datetime import datetime
import re

import requests
import toml
from streamlit import success


class MessagePush():
    def __init__(self,message):
        self._cfg = toml.load('../../settings/configs.toml')
        self._robot_url = self._cfg['DINGROBOT']['robot_url']
        self.message = message

    def success_text_push(self):
        for i in self.message:
            guotie_sku = i["guotie_sku"]
            sku_name = i["sku_name"].replace(" ","").replace("\n","")
            guotie_price = i["guotie_price"]
            jd_url = i["jd_url"]
            jingdong_sell_price = i["jingdong_sell_price"]
            print(jingdong_sell_price,guotie_price)
            remark = i["remark"]
            if float(jingdong_sell_price) < float(guotie_price):
                if remark == "该商品京东链接已下柜，请及时更换京东比价链接":
                    remark = "该商品京东链接已下柜，请及时更换京东比价链接"
                else:remark = "国铁商品价格高于京东商品价格"
                success_robot_body_at = {
                    "msgtype": "markdown",
                    "markdown": {
                        "title": "通知：国铁&京东——比价异常",
                        "text": f"### 通知：国铁&京东——比价异常\n\n"
                                f"- #### 国铁商品编码：<font color=\"#E4080A\">{guotie_sku}</font>\n\n"
                                f"- #### 国铁商品名字：<font color=\"#0000CD\">{sku_name}</font>\n\n"
                                f"- #### 国铁商品价格：<font color=\"#E4080A\">{guotie_price}</font>\n\n"
                                f"- #### 京东商品链接：<font color=\"#0000CD\">{jd_url}</font>\n\n"
                                f"- #### 京东商品价格：<font color=\"#E4080A\">{jingdong_sell_price}</font>\n\n"
                                f"- #### 通知原因：<font color=\"#E4080A\">{remark}</font>\n\n"
                                f"@xxxxxxxxxxx",
                    },
                    "at": {
                        "isAtAll": "false",
                        "atMobiles": ["xxxxxxxxxxx"]
                    }
                }
                # print(success_robot_body_at)
                res = requests.post(url=self._robot_url, json=success_robot_body_at)
    #
    # def admin_error_text_push(self):
    #     code = self.message["code"]
    #     result = self.message["result"]
    #     error = self.message["error"]
    #     retry = self.message["retry"]
    #
    #     if code == 2:
    #         error_robot_body_at = {
    #             "msgtype": "markdown",
    #             "markdown": {
    #                 "title": "国铁Price&京东Price——程序终止运行",
    #                 "text": f"### 国铁Price&京东Price——程序终止运行\n\n"
    #                         f"- #### 程序报错代码：<font color=\"#0000CD\">{code}</font>\n\n"
    #                         f"- #### 程序运行状态：<font color=\"#E4080A\">{result}</font>\n\n"
    #                         f"- #### 程序报错信息：<font color=\"#0000CD\">{error}</font>\n\n"
    #                         f"- #### 程序已重试：<font color=\"#0000CD\">{retry}</font>\n\n"
    #                         f"@xxxxxxxxxxx",
    #             },
    #             "at": {
    #                 "isAtAll": "false",
    #                 "atMobiles": ["xxxxxxxxxxx"]
    #             }
    #         }
    #         res = requests.post(url=self._robot_url, json=error_robot_body_at)
    # def admin_info_text_push(self):
    #     code = self.message["code"]
    #     status = self.message["status"]
    #     result = self.message["result"]
    #     update = self.message["update"]
    #     message_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    #     if code == 3:
    #         error_robot_body_at = {
    #             "msgtype": "markdown",
    #             "markdown": {
    #                 "title": "京东商城订单通知——程序运行情况通知",
    #                 "text": f"### 京东商城订单通知——程序运行情况通知\n\n"
    #                         f"- #### 程序代码：<font color=\"#E4080A\">{code}</font>\n\n"
    #                         f"- #### 程序运行状态：<font color=\"#0000CD\">{result}</font>\n\n"
    #                         f"- #### 处理结果：<font color=\"#0000CD\">{update}</font>\n\n"
    #                         f"- 机器人通知时间：<font color=\"#4F4F4F\">{message_time}</font>\n\n"
    #                         f"@xxxxxxxxxxx",
    #             },
    #             "at": {
    #                 "isAtAll": "false",
    #                 "atMobiles": ["xxxxxxxxxxx"]
    #             }
    #         }
    #         res = requests.post(url=self._robot_url, json=error_robot_body_at)




